//
//  myViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/11/14.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "MainTabBarController.h"
#import "qyxxoneViewController.h"
#import "cglistViewController.h"
#import "getProductListViewController.h"
#import "shangpinzulistViewController.h"
#import "caigoulistViewController.h"
#import "findpasswordfirstViewController.h"
#import "findpasswordemailViewController.h"
#import "wmimaViewController.h"
#import "rongzilistViewController.h"
#import "mimaViewController.h"
#import "myViewController.h"
#import "rongzixqViewController.h"
#import "OALoginViewController.h"
#import "addProductListViewController.h"
@interface myViewController ()

@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _uparray = @[NSLocalizedString(@"商品列表", nil),NSLocalizedString(@"添加商品", nil),NSLocalizedString(@"商品组", nil),NSLocalizedString(@"我的-采购信息", nil)];
        _uppicarray = @[@"我的-商品列表",@"我的-添加商品",@"我的-商品组",@"我的-采购信息"];
                _downpicarray = @[@"我的-语言切换",@"我的-企业信息",@"我的-修改密码",@"我的-退出"];

        _downarray = @[NSLocalizedString(@"切换语言", nil),NSLocalizedString(@"企业信息", nil),NSLocalizedString(@"修改密码", nil),NSLocalizedString(@"退出", nil)];


    
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
 self.navigationController.navigationBar.barTintColor = COLORRGB(0x3F51B5);
      self.title = NSLocalizedString(@"我的", nil);
      self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
       [self initview];
       [self refreshData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"refreshOADetailData" object:nil];

    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
      self.navigationController.navigationBar.hidden = YES;
//    [self.navigationController setNavigationBarHidden:YES animated:animated];


}
- (void)refreshData{


//label1.text = @"1111111";
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body></body></message>",userId,password,website,language,BAtype];
  bodyStr= [bodyStr URLEncodedString];
      NSString * path = [NSString stringWithFormat:@"%@%@",BAip,blogin];
       NSArray * array = @[@{@"arg0":bodyStr}];

[HttpTools requestWithURL:path arrays:array method:@"myPage" success:^(id JSON) {
                  if([[NSString stringWithFormat:@"%@",JSON[@"code"]] isEqualToString:@"200"]){
        NSDictionary *dic = JSON[@"userInfo"];
             [[NSUserDefaults standardUserDefaults] setObject:JSON[@"productnum"] forKey:@"productnum"];
             [[NSUserDefaults standardUserDefaults] setObject:JSON[@"receivenum"] forKey:@"receivenum"];
             [[NSUserDefaults standardUserDefaults] setObject:JSON[@"demandnum"] forKey:@"demandnum"];
if([[JSON allKeys] containsObject:@"company"]){
NSString *state =[NSString stringWithFormat:@"%@",JSON[@"company"][@"state"]];
   [[NSUserDefaults standardUserDefaults] setObject:state forKey:@"state"];

}
[self values];
}else{
                  [self.view makeToast:JSON[@"msg"]];

}
} failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];


   

}
- (void)values{
UILabel *label1 = (UILabel *)[self.view viewWithTag:100];
UILabel *label2 = (UILabel *)[self.view viewWithTag:101];
UILabel *label3 = (UILabel *)[self.view viewWithTag:102];
UILabel *label4 = (UILabel *)[self.view viewWithTag:103];

                    NSString *realname = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

      NSString *state =[[NSUserDefaults standardUserDefaults] objectForKey:@"state"];
    
      if([state isEqualToString:@"0"]){
      label4.text = [NSString stringWithFormat:@"%@[%@]",realname,NSLocalizedString(@"审核不通过", nil)];
  
  }else if ([state isEqualToString:@"1"]){
    label4.text = [NSString stringWithFormat:@"%@[%@]",realname,NSLocalizedString(@"审核通过", nil)];

  
  }else if ([state isEqualToString:@"2"]){
    label4.text = [NSString stringWithFormat:@"%@[%@]",realname,NSLocalizedString(@"待审核", nil)];

  }else{
      label4.text = [NSString stringWithFormat:@"%@",realname];

  
  }

NSString *productnum =[[NSUserDefaults standardUserDefaults] objectForKey:@"productnum"];
   label1.text = [NSString stringWithFormat:@"%@",productnum] ;
  
      NSString *receivenum =[[NSUserDefaults standardUserDefaults] objectForKey:@"receivenum"];
      label2.text =[NSString stringWithFormat:@"%@",receivenum] ;

      NSString *demandnum =[[NSUserDefaults standardUserDefaults] objectForKey:@"demandnum"];

         label3.text = [NSString stringWithFormat:@"%@",demandnum];
}
- (void)viewDidDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];

    self.navigationController.navigationBarHidden = NO;


}
- (void)initview{

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, 200)];
view.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];
//view.backgroundColor = [UIColor whiteColor];
UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, (view.height-100), KScreenWidth, 100)];
bgimage.image = [UIImage imageNamed:@"白弧度"];
[view addSubview:bgimage];
//_image = [[UIImageView alloc]initWithFrame:CGRectMake((KScreenWidth-100)/2, (view.height-100)/2-20, 100, 100)];
_image = [[UIImageView alloc]initWithFrame:CGRectMake((KScreenWidth-100)/2, (view.height-150), 100, 100)];

_image.image = [UIImage imageNamed:@"我的-头像"];
[view addSubview:_image];


UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,_image.bottom, KScreenWidth-20, 20)];
//       label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
                NSString *realname = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
                NSString *comname = [[NSUserDefaults standardUserDefaults] objectForKey:@"companyName"];

label.text = comname;
//label.text = @"xxxxxxxxxx";

     label.font = [UIFont systemFontOfSize:15];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHexString:@"0aa0f6"];
[view addSubview:label];

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10,label.bottom+0, KScreenWidth-20, 20)];
label1.tag = 103;
//label1.text = realname;
//label1.text = @"xxxxxxxxxx";
     label1.font = [UIFont systemFontOfSize:13];
label1.textAlignment = NSTextAlignmentCenter;
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor colorWithHexString:@"484848"];
[view addSubview:label1];
[self.view addSubview:view];

NSArray *array = @[NSLocalizedString(@"商品数量", nil),NSLocalizedString(@"收件箱", nil),NSLocalizedString(@"采购信息", nil)];
for (int i = 0 ; i < 3; i++) {
UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/3*i,view.bottom+1, KScreenWidth/3, 19)];
label.tag = 100+i;
label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor colorWithHexString:@"0a62ea"];
   label.font = [UIFont boldSystemFontOfSize:14];
   label.textAlignment = 1;
                   NSString *comname = [[NSUserDefaults standardUserDefaults] objectForKey:@"companyName"];

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( KScreenWidth/3*i,label.bottom, KScreenWidth/3, 20);
button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
button.backgroundColor = [UIColor whiteColor];
[button setTitleColor:[UIColor colorWithHexString:@"484848"] forState:UIControlStateNormal];
button.tag = i;
[button setTitle:array[i] forState:UIControlStateNormal];
[self.view addSubview:label];
[self.view addSubview:button];


}
_view3 = [[UITableView alloc]initWithFrame:CGRectMake(0, view.bottom+40, KScreenWidth, KScreenHeight-view.height-40-kTabBarHeight) style:UITableViewStylePlain];
_view3.dataSource = self;
_view3.delegate = self;
self.view3.bounces = NO;
_view3.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];

//_view3.separatorStyle = NO;

[self.view addSubview:_view3];
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      switch (section) {
        case 0:
            return 4;
            break;
        case 1:
            return 4;
        default:
            return 1;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    bgView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
        return bgView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
     static NSString *CellIdentifier = @"ImageOnRightCell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
cell.imageView.image = [UIImage imageNamed:_uppicarray[indexPath.row]];
cell.textLabel.text = _uparray[indexPath.row];
cell.textLabel.textColor = [UIColor colorWithHexString:@"484848"];
cell.textLabel.font = [UIFont systemFontOfSize: 14.0];

        return cell;
}else{
 static NSString *CelldownIdentifier = @"ImagedownRightCell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CelldownIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CelldownIdentifier];
              cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
cell.imageView.image = [UIImage imageNamed:_downpicarray[indexPath.row]];
cell.textLabel.text = _downarray[indexPath.row];
cell.textLabel.textColor = [UIColor colorWithHexString:@"484848"];
cell.textLabel.font = [UIFont systemFontOfSize: 14.0];

        return cell;

}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0){
if (indexPath.row ==0){
getProductListViewController *vc = [[getProductListViewController alloc]init];

vc.hidesBottomBarWhenPushed  = YES;
[self.navigationController pushViewController:vc animated:YES];
}if (indexPath.row ==1){
addProductListViewController *vc = [[addProductListViewController alloc]init];

vc.hidesBottomBarWhenPushed  = YES;
[self.navigationController pushViewController:vc animated:YES];
}if (indexPath.row ==2){
shangpinzulistViewController*vc = [[shangpinzulistViewController alloc]init];

vc.hidesBottomBarWhenPushed  = YES;
[self.navigationController pushViewController:vc animated:YES];

}if (indexPath.row ==3){
cglistViewController*vc = [[cglistViewController alloc]init];

vc.hidesBottomBarWhenPushed  = YES;
[self.navigationController pushViewController:vc animated:YES];

}
}

if (indexPath.section ==1){
if (indexPath.row ==0){
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];

if([language isEqualToString:@"0"]){
      [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"language"];
           [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"website"];

    [self changeLanguageTo:@"en"];
MainTabBarController *tar = [[MainTabBarController alloc]init];

                   self.view.window.rootViewController = tar;

}else{
  [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"language"];
    [self changeLanguageTo:@"zh-Hans"];
MainTabBarController *tar = [[MainTabBarController alloc]init];
                          [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"website"];

                   self.view.window.rootViewController = tar;

}


}
if (indexPath.row ==1){
qyxxoneViewController *vc = [[qyxxoneViewController alloc]init];
vc.hidesBottomBarWhenPushed  =  YES;
[self.navigationController pushViewController:vc animated:YES];
}
if (indexPath.row ==2){
mimaViewController *vc = [[mimaViewController alloc]init];
//findpasswordemailViewController *vc = [[findpasswordemailViewController alloc]init];

vc.hidesBottomBarWhenPushed  = YES;
[self.navigationController pushViewController:vc animated:YES];
}
if (indexPath.row ==3){
 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"是否退出系统", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"控件-取消", nil) otherButtonTitles:NSLocalizedString(@"控件-确定", nil), nil];
    [alertView show];

}
}


}
- (void)changeLanguageTo:(NSString *)language {
    // 设置语言
    [NSBundle setLanguage:language];
    
    // 然后将设置好的语言存储好，下次进来直接加载
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:@"myLanguage"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 我们要把系统windown的rootViewController替换掉
//    ZHTabBarController *tab = [[ZHTabBarController alloc] init];
//    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
//    // 跳转到设置页
//    tab.selectedIndex = 2;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){
// [_helper logoutVpn];
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
          DanLi *danli = [[DanLi alloc] init];
    danli.stay = @"no";
 OALoginViewController * loginVC =  [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
    self.view.window.rootViewController = loginVC;


}
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
